package com.ruoyi.web.controller.energy;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.energy.domain.EmsEnergyReport;
import com.ruoyi.energy.service.IEmsEnergyReportService;

/**
 * 能源报表Controller
 * 
 * @author ruoyi
 * @date 2024-01-20
 */
@RestController
@RequestMapping("/energy/report")
public class EmsEnergyReportController extends BaseController {
    
    @Autowired
    private IEmsEnergyReportService emsEnergyReportService;

    /**
     * 查询能源报表列表
     */
    @PreAuthorize("@ss.hasPermi('energy:report:list')")
    @GetMapping("/list")
    public TableDataInfo list(EmsEnergyReport emsEnergyReport) {
        startPage();
        List<EmsEnergyReport> list = emsEnergyReportService.selectEmsEnergyReportList(emsEnergyReport);
        return getDataTable(list);
    }

    /**
     * 导出能源报表列表
     */
    @PreAuthorize("@ss.hasPermi('energy:report:export')")
    @Log(title = "能源报表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EmsEnergyReport emsEnergyReport) {
        List<EmsEnergyReport> list = emsEnergyReportService.selectEmsEnergyReportList(emsEnergyReport);
        ExcelUtil<EmsEnergyReport> util = new ExcelUtil<EmsEnergyReport>(EmsEnergyReport.class);
        util.exportExcel(response, list, "能源报表数据");
    }

    /**
     * 获取能源报表详细信息
     */
    @PreAuthorize("@ss.hasPermi('energy:report:query')")
    @GetMapping(value = "/{reportId}")
    public AjaxResult getInfo(@PathVariable("reportId") Long reportId) {
        EmsEnergyReport report = new EmsEnergyReport();
        report.setReportId(reportId);
        List<EmsEnergyReport> list = emsEnergyReportService.selectEmsEnergyReportList(report);
        return AjaxResult.success(list.isEmpty() ? null : list.get(0));
    }

    /**
     * 新增能源报表
     */
    @PreAuthorize("@ss.hasPermi('energy:report:add')")
    @Log(title = "能源报表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EmsEnergyReport emsEnergyReport) {
        return toAjax(emsEnergyReportService.insertEmsEnergyReport(emsEnergyReport));
    }

    /**
     * 修改能源报表
     */
    @PreAuthorize("@ss.hasPermi('energy:report:edit')")
    @Log(title = "能源报表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EmsEnergyReport emsEnergyReport) {
        return toAjax(emsEnergyReportService.updateEmsEnergyReport(emsEnergyReport));
    }

    /**
     * 删除能源报表
     */
    @PreAuthorize("@ss.hasPermi('energy:report:remove')")
    @Log(title = "能源报表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{reportIds}")
    public AjaxResult remove(@PathVariable Long[] reportIds) {
        return toAjax(emsEnergyReportService.deleteEmsEnergyReportByReportIds(reportIds));
    }

    /**
     * 根据日期范围查询报表
     */
    @PreAuthorize("@ss.hasPermi('energy:report:list')")
    @GetMapping("/dateRange")
    public AjaxResult getReportByDateRange(@RequestParam String startDate, 
                                          @RequestParam String endDate,
                                          @RequestParam(required = false) String reportType) {
        List<EmsEnergyReport> list = emsEnergyReportService.selectEmsEnergyReportByDateRange(startDate, endDate, reportType);
        return AjaxResult.success(list);
    }

    /**
     * 查询设备能耗趋势
     */
    @PreAuthorize("@ss.hasPermi('energy:report:list')")
    @GetMapping("/trend/{deviceId}")
    public AjaxResult getEnergyTrend(@PathVariable Long deviceId,
                                    @RequestParam(defaultValue = "1") String reportType,
                                    @RequestParam(defaultValue = "30") Integer limit) {
        List<EmsEnergyReport> list = emsEnergyReportService.selectEnergyTrendByDevice(deviceId, reportType, limit);
        return AjaxResult.success(list);
    }

    /**
     * 查询能耗排行榜
     */
    @PreAuthorize("@ss.hasPermi('energy:report:list')")
    @GetMapping("/ranking")
    public AjaxResult getEnergyRanking(@RequestParam(defaultValue = "1") String reportType,
                                      @RequestParam(required = false) String reportDate,
                                      @RequestParam(defaultValue = "10") Integer limit) {
        List<EmsEnergyReport> list = emsEnergyReportService.selectEnergyRanking(reportType, reportDate, limit);
        return AjaxResult.success(list);
    }

    /**
     * 查询综合统计数据
     */
    @PreAuthorize("@ss.hasPermi('energy:report:list')")
    @GetMapping("/stats")
    public AjaxResult getComprehensiveStats(@RequestParam(defaultValue = "1") String reportType,
                                           @RequestParam(required = false) String reportDate) {
        Map<String, Object> stats = emsEnergyReportService.selectComprehensiveStats(reportType, reportDate);
        return AjaxResult.success(stats);
    }

    /**
     * 生成报表数据
     */
    @PreAuthorize("@ss.hasPermi('energy:report:generate')")
    @Log(title = "生成报表", businessType = BusinessType.INSERT)
    @PostMapping("/generate")
    public AjaxResult generateReport(@RequestParam String reportType, @RequestParam String reportDate) {
        int result = emsEnergyReportService.generateReportData(reportType, reportDate);
        return AjaxResult.success("成功生成 " + result + " 条报表数据");
    }

    /**
     * 查询仪表盘统计数据
     */
    @PreAuthorize("@ss.hasPermi('energy:report:list')")
    @GetMapping("/dashboard")
    public AjaxResult getDashboardStats() {
        Map<String, Object> dashboard = emsEnergyReportService.selectDashboardStats();
        return AjaxResult.success(dashboard);
    }
} 